//
//  PaperAnalyScrollView.m
//  智云题库
//
//  Created by 毕武侠 on 2017/3/17.
//  Copyright © 2017年 bwx. All rights reserved.
//

#import "PaperAnalyScrollView.h"
#import "NALLabelsMatrix.h"
#import "PaperAnalyModel.h"
#import "JCTagListView.h"
#import "PaperHardStarView.h"

@interface PaperAnalyScrollView ()

@property (nonatomic, strong) UIView *lineView;
@property (nonatomic, strong) UILabel *analyTitleLabel;
@property (nonatomic, strong) NALLabelsMatrix *mstrixView;   // 表格
@property (nonatomic, strong) PaperAnalyModel *model;
@property (nonatomic, strong) YYLabel *analyLabel;           // 解析

@property (nonatomic, strong) UILabel *tagTitleLabel;        // 考点标题
@property (nonatomic, strong) JCTagListView *tagListView;    // 考点

@property (nonatomic, strong) UILabel *hardTitleLabel;       // 难度标题
@property (nonatomic, strong) PaperHardStarView *hardView;   // 难度View
@property (nonatomic, strong) UILabel *wrongTitleLabel;      // 易错标题
@property (nonatomic, strong) UILabel *wrongLabel;           // 易错View


#pragma mark - 问答题
@property (nonatomic, strong) YYLabel *QA_Label;           // 问答题答案


@end

@implementation PaperAnalyScrollView

- (void)setupConfigure {
    [super setupConfigure];
    
    [self addSubview:self.lineView];
    [self addSubview:self.analyTitleLabel];

    [self addSubview:self.analyLabel];
    [self addSubview:self.tagTitleLabel];
    [self addSubview:self.tagListView];
//    [self addSubview:self.hardTitleLabel];
//    [self addSubview:self.hardView];
//    [self addSubview:self.wrongTitleLabel];
//    [self addSubview:self.wrongLabel];
}

- (void)bindModel:(PaperAnalyModel *)model {
    _model = model;
    
    [self refreshView];
}

// 更新页面
- (void)refreshView {
    [super refreshView];
    
    CGFloat top = self.contentSize.height - 16;
    self.lineView.top = top;
    top = self.lineView.bottom + 12;
    if (self.model.showPaperAnaly == NO && self.model.showAnswerAnaly == NO) {
        self.analyTitleLabel.hidden = YES;
    } else {
        self.analyTitleLabel.top = top;
        self.analyTitleLabel.hidden = NO;
        top = self.analyTitleLabel.bottom + 12;
    }
//    NSLog(@"%@",self.model.right_answer);

    if (self.model.ques_model != 2) {//当不是问答题的时候显示选择题格式答案
        if (self.model.showPaperAnaly == YES) {
            [self addSubview:self.mstrixView];
            self.mstrixView.top = top;
            self.mstrixView.hidden = NO;
            [self.mstrixView setRecord:@[self.model.right_answer ? : @"", self.model.user_answer ? : @""] atIndex:1];
            top = self.mstrixView.bottom + 16;
        } else {
            self.mstrixView.hidden = YES;
        }
    }else{// 当是问答题显示模式
        if (self.model.showPaperAnaly == YES) {
            [self addSubview:self.QA_Label];
            self.QA_Label.top = top;
            self.QA_Label.hidden = NO;
            self.QA_Label.height = self.model.wendaLayout.textBoundingSize.height;
            self.QA_Label.textLayout = self.model.wendaLayout;
            top = self.QA_Label.bottom + 10.0f;
        }else{
            self.QA_Label.hidden = YES;

        }
        
    }
    
    
    if (self.model.showAnswerAnaly == YES) {
        self.analyLabel.top = top;
        self.analyLabel.hidden = NO;
        self.analyLabel.height = self.model.analyLayout.textBoundingSize.height;
        self.analyLabel.textLayout = self.model.analyLayout;
        top = self.analyLabel.bottom + 18.0f;
    } else {
        self.analyLabel.hidden = YES;
    }
    
    if (self.model.showKnowledge == YES) {
        self.tagTitleLabel.top = top;
        self.tagTitleLabel.hidden = NO;
        
        self.tagListView.top = self.analyLabel.bottom + 18.0f;
        self.tagListView.hidden = NO;
        
        //重复。。。
        [self.tagListView.tags removeAllObjects];
        [self.tagListView.tags addObjectsFromArray:self.model.knows_name];
#warning  王旭修改[self.tagListView reloadData]
        [self.tagListView.collectionView reloadData];
        
        
        self.hardTitleLabel.top = self.tagListView.bottom + 4.0;
        self.hardTitleLabel.hidden = NO;
        
        self.hardView.top = self.hardTitleLabel.top;
        self.hardView.index = self.model.difficulty;
        self.hardView.hidden = NO;
        
        self.wrongTitleLabel.top = self.hardTitleLabel.bottom + 12.0f;
        self.wrongTitleLabel.hidden = NO;
        
        self.wrongLabel.top = self.wrongTitleLabel.top;
        self.wrongLabel.attributedText = self.model.attributedText;
        self.wrongLabel.hidden = NO;
    } else {
        self.tagTitleLabel.hidden = YES;
        self.tagListView.hidden = YES;
        self.hardTitleLabel.hidden = YES;
        self.hardView.hidden = YES;
        self.wrongTitleLabel.hidden = YES;
        self.wrongLabel.hidden = YES;
    }
    
    self.contentSize = CGSizeMake(SCREEN_WIDTH, self.analyLabel.bottom + 32);
}

#pragma mark - getter

- (UIView *)lineView {
    if (_lineView == nil) {
        _lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 1)];
        _lineView.backgroundColor = RGB(234, 234, 234);
    }
    return _lineView;
}

- (UILabel *)analyTitleLabel {
    if (_analyTitleLabel == nil) {
        _analyTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(12, 0, SCREEN_WIDTH - 24, 32)];
        _analyTitleLabel.text = @"作答解析";
        _analyTitleLabel.textColor = [UIColor darkGrayColor];
    }
    return _analyTitleLabel;
}

- (NALLabelsMatrix *)mstrixView {
    if (_mstrixView == nil) {
        NSNumber *columnsWidth = [NSNumber numberWithFloat:(SCREEN_WIDTH  - 24.0) / 2.0];
        _mstrixView = [[NALLabelsMatrix alloc] initWithFrame:CGRectMake(12, 0, SCREEN_WIDTH, 100) andColumnsWidths:@[columnsWidth, columnsWidth]];
        [_mstrixView addRecord:@[@"正确答案", @"我的答案"]];
        [_mstrixView addRecord:@[@"", @""]];
    }
    return _mstrixView;
}

- (YYLabel *)analyLabel {
    if (_analyLabel == nil) {
        _analyLabel = [[YYLabel alloc] initWithFrame:CGRectMake(12, 0, SCREEN_WIDTH - 24.0, 32.0f)];
        _analyLabel.textVerticalAlignment = YYTextVerticalAlignmentTop;
        _analyLabel.displaysAsynchronously = YES;
        _analyLabel.ignoreCommonProperties = YES;
        _analyLabel.fadeOnAsynchronouslyDisplay = NO;
        _analyLabel.fadeOnHighlight = NO;
    }
    return _analyLabel;
}

- (YYLabel *)QA_Label{
    if (_QA_Label == nil) {
        _QA_Label = [[YYLabel alloc] initWithFrame:CGRectMake(12, 0, SCREEN_WIDTH - 24.0, 32.0f)];
        _QA_Label.textVerticalAlignment = YYTextVerticalAlignmentTop;
        _QA_Label.displaysAsynchronously = YES;
        _QA_Label.ignoreCommonProperties = YES;
        _QA_Label.fadeOnAsynchronouslyDisplay = NO;
        _QA_Label.fadeOnHighlight = NO;
    }
    return _QA_Label;
}

- (UILabel *)tagTitleLabel {
    if (_tagTitleLabel == nil) {
        _tagTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(12, 0, 80.0, 45)];
        _tagTitleLabel.text = @"考        点:";
        _tagTitleLabel.textColor = [UIColor darkGrayColor];
    }
    return _tagTitleLabel;
}

- (JCTagListView *)tagListView {
    if (_tagListView == nil) {
        _tagListView = [[JCTagListView alloc] initWithFrame:CGRectMake(96.0, 0, SCREEN_WIDTH - 100, 24.0)];
        _tagListView.tagStrokeColor = MainThemeColor;
        _tagListView.tagTextColor = MainThemeColor;
        _tagListView.tagCornerRadius = 12.0f;
    }
    return _tagListView;
}

- (UILabel *)hardTitleLabel {
    if (_hardTitleLabel == nil) {
        _hardTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(12, 0, 80.0, 24)];
        _hardTitleLabel.text = @"考试难度:";
        _hardTitleLabel.textColor = [UIColor darkGrayColor];
    }
    return _hardTitleLabel;
}

- (PaperHardStarView *)hardView {
    if (_hardView == nil) {
        _hardView = [[PaperHardStarView alloc] initWithFrame:CGRectMake(96.0, 0, SCREEN_WIDTH - 100, 24.0)];
    }
    return _hardView;
}

- (UILabel *)wrongTitleLabel {
    if (_wrongTitleLabel == nil) {
        _wrongTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(12, 0, 80.0, 24)];
        _wrongTitleLabel.text = @"易错项是:";
        _wrongTitleLabel.textColor = [UIColor darkGrayColor];
    }
    return _wrongTitleLabel;
}

- (UILabel *)wrongLabel {
    if (_wrongLabel == nil) {
        _wrongLabel = [[UILabel alloc] initWithFrame:CGRectMake(96.0, 0, SCREEN_WIDTH - 100.0, 24.0)];
        _wrongLabel.textColor = [UIColor darkGrayColor];
    }
    return _wrongLabel;
}

@end
